#ifndef SERIALCONTROLLER_H
#define SERIALCONTROLLER_H

#include <QObject>
#include <QList>
#include "qserialproto.h"
#include <vector>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>


#define OK 0
#define NULLPOINTER -1

/*
 * 数据传输方式
 data -> controler -> proto -> listener
*/
using namespace  std;
class SerialController : public QObject
{
    Q_OBJECT
public slots:
    void ReadyRead();
    void AboutClose();
public:
    typedef  class {
    public:
        virtual int OnDataRecv(QByteArray){
            return -1;
        };
    }SerialListener;

    typedef  enum  {
        ERROR_OPEN = -1,
        ERROR_SEND = -2,
    }SerialError;
    explicit SerialController(QObject *parent = nullptr);
    int OpenSerial(QString port,QString baudRate, QString dataBits, QString stopBits, QString flow);

    int CloseSerial();
    QList<QSerialPortInfo> GetPorts();
    int SetProto(QSerialProto *);
    int SetListener(SerialListener *);

    int SendData(char *,uint8_t len);
    void GetSetting(QString baudRate, QString dataBits, QString stopBits, QString flow);
signals:
private:
    QList<QSerialPortInfo> mPorts;
    bool mConnected;
    QSerialPort  *mCurrentPort;
    QSerialProto *mProto;
    QString mPortName;
    uint32_t mBaudrate;
    QSerialPort::DataBits   mDataBits;
    QSerialPort::Parity mParity;
    QSerialPort::StopBits mStopBits;
    QSerialPort::FlowControl mFlow;
    SerialListener *mListener;
};

#endif // SERIALCONTROLLER_H
